package com.study.mybatis.service;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.study.mybatis.entity.*;
import com.study.mybatis.mapper.StClassMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@Service
public class StClassServiceImpl implements StClassService {
    @Autowired
    private StClassMapper stClassMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void addClass(StClass stClass) {
        stClassMapper.insert(stClass);
    }

    @Override
    public void updateClass(StClass stClass) {
        stClassMapper.update(stClass);
    }

    @Override
    public void removeClass(Integer id) {
        stClassMapper.deleteById(id);
    }

    @Override
    public StClass getClassById(Integer id) {
        return stClassMapper.selectById(id);
    }

    @Override
    public List<StClass> getAllClasses() {
        return stClassMapper.selectAll();
    }

    @Override
    @Transactional
    public int update(StClass stClass) {
        stClassMapper.selectById(stClass.getId());
        System.out.println("更新");
        stClassMapper.update(stClass);
        return 0;
    }

    public String query(){
        String s = "{ \n" +
                "  \"model\": \"Publisher/Repository/qwen1_5-7b-chat-q2_k.gguf\",\n" +
                "  \"messages\": [ \n" +
                "    { \"role\": \"system\", \"content\": \"Always answer in rhymes.\" },\n" +
                "    { \"role\": \"user\", \"content\": \"我好想你？\" }\n" +
                "  ], \n" +
                "  \"temperature\": 0.7, \n" +
                "  \"max_tokens\": -1,\n" +
                "  \"stream\": true\n" +
                "}";
        JSONObject jsonObject = JSONUtil.parseObj(s);
        StringBuilder sb = new StringBuilder();
        ResponseEntity<String> ss = restTemplate.postForEntity("http://localhost:1234/v1/chat/completions",
                jsonObject, String.class);
      //  System.out.println(ss.getBody());
        String[] split = ss.getBody().split("\\n");
        for (String s1 : split) {
            String replace = s1.replace("data:", "");
        //    System.out.println(replace);
            if(!StringUtils.isBlank(replace)&&replace.contains("{")){
                ChatCompletionChunk response = JSONUtil.toBean(replace, ChatCompletionChunk.class);
                for (Choice choice : response.getChoices()) {
                    String content = choice.getDelta().getContent();
                    // System.out.print(content);
                    sb.append(content);
                }
            }
        }
        System.out.println(sb.toString());
        return sb.toString();
    }

}
